/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package old;

import java.awt.Toolkit;
import twitter4j.*;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;  
import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
  

  public class TwitterApp extends JFrame {
	  
      public TwitterApp() throws TwitterException, IOException {
    	  
        super("Simple Twitter App");
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
  
        //Authentication steps
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer("jaBct5meAX3L676Tq00HA", "rA7FhUkPrAblbfrjy01sKuIJJKYYh9AGx9pthzd4s");
        RequestToken requestToken = twitter.getOAuthRequestToken();
        AccessToken accessToken = null;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        while (null == accessToken) {
          System.out.println("Open the following URL and grant access to your account:");
          System.out.println(requestToken.getAuthorizationURL());
          System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
          String pin = br.readLine();
          try{
             if(pin.length() > 0){
               accessToken = twitter.getOAuthAccessToken(requestToken, pin);
             }else{
               accessToken = twitter.getOAuthAccessToken();
             }
          } catch (TwitterException te) {
            if(401 == te.getStatusCode()){
              System.out.println("Unable to get the access token.");
            }else{
              te.printStackTrace();
            }
          }
        }
        
        this.add(new SearchTweetsFrame(twitter));
       
                   
      }
  
      public static void main(String[] args) throws MalformedURLException, TwitterException {
          TwitterApp application;
		try {
			application = new TwitterApp();
			application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			application.setSize(Toolkit.getDefaultToolkit().getScreenSize());
			application.setVisible(true);
  
		} catch (IOException e) {
			e.printStackTrace();
		}
          
      }
      
  }